package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Alfred
 * @description  标签控制层
 * @date 2021/4/22 20:32
 */
@RestController
@RequestMapping("/label")
@CrossOrigin //跨域处理
public class LabelController {

    @Autowired
    private LabelService labelService;

    /**
     * fetch data by rule id
     *
     * @description  查询全部列表
     * @param
     * @return
     */
    @GetMapping
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询成功",labelService.findAll());
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID查询标签
     * @param
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result findById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",labelService.findById(id));
    }

    /**
     * fetch data by rule id
     *
     * @description  增加标签
     * @param
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Label label){
        //System.out.println(label+"----------------------------");
        labelService.add(label);
        return new Result(true,StatusCode.OK,"增加成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  修改标签
     * @param
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Label label,@PathVariable String id){

        label.setId(id);
        labelService.update(label);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  删除标签
     * @param
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id){
        labelService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据条件查询标签
     * @param searchMap
     * @return
     */
    @PostMapping(value = "/search")
    public Result findSearch(@RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",labelService.findSearch(searchMap));
    }

    /**
     * fetch data by rule id
     *
     * @description  条件 + 分页查询
     * @param  searchMap
     * @param  size
     * @param  page
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result findSearch(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size){
        Page<Label> pageList = labelService.findSearch(searchMap, size, page);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }
}
